package com.monkey.web.platform.infrastructure;

import com.monkey.core.exception.MonkeyException;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * Project  : monkey
 * Author   : Wu Tian Qiang
 * Date     : 2016/9/27
 */
public class ParameterValid {

    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new MonkeyException(message);
        }
    }

    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new MonkeyException(message);
        }
    }

    public static void isFalse(boolean expression, String message) {
        if (!expression) {
            throw new MonkeyException(message);
        }
    }

    public static void isNull(Object object, String message) {
        if (object != null) {
            throw new MonkeyException(message);
        }
    }

    public static void hasLength(String text, String message) {
        if (!StringUtils.hasLength(text)) {
            throw new MonkeyException(message);
        }
    }

    public static void hasText(String text, String message) {
        if (!StringUtils.hasText(text)) {
            throw new MonkeyException(message);
        }
    }

    public static void notEmpty(Object[] array, String message) {
        if (ObjectUtils.isEmpty(array)) {
            throw new MonkeyException(message);
        }
    }

    public static void notEmpty(Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new MonkeyException(message);
        }
    }

    public static void notEmpty(Map<?, ?> map, String message) {
        if (CollectionUtils.isEmpty(map)) {
            throw new MonkeyException(message);
        }
    }

    public static void size(Double value, Integer min, Integer max, String message) {
        if (value < min || value > max) {
            throw new MonkeyException(message);
        }
    }

    public static void pattern(String value, String pattern, String message) {
        if (!value.matches(pattern)) {
            throw new MonkeyException(message);
        }
    }

    public static void min(Double value, Integer min, String message) {
        if (value < min) {
            throw new MonkeyException(message);
        }
    }

    public static void max(Double value, Integer max, String message) {
        if (value > max) {
            throw new MonkeyException(message);
        }
    }


}
